<?php

include_once dirname(__FILE__) . '/../system/settings.php';
if ($_POST) {
    $id = $_POST['id'];
    $url = UrlAmigavel($_POST['name']);
    $name = $_POST['name'];
    if ($_POST['detached']) {
        $detached = $_POST['detached'];
    } else {
        $detached = 0;
    }
    
    //verificação da URL Amigável
    if (isset($codigo) and $codigo != "") {
        $reg = MarcasControl::Select((int)$codigo);
        $urli = $reg[0]['url'];
        if ($urli == $url) {
            unset($url);
        }
    }
    if (isset($name)) {
        $r = MarcasControl::Select((string)$url);
        foreach($r as $list){
            $n++;
            $r = MarcasControl::Select((string)$url."-".$n);
        }
        if ($n) {
            $url .= "-" . $n;
        }
    }
    if (!isset($url)) {
        $url = $urli;
    }    
    
    if (!empty($id)) {
        if (!MarcasControl::Update($url, $name, $detached, $id)) {
            header("location: " . $_POST['redirect']);
            exit();
        }
    } else {
        if (!MarcasControl::Insert($url, $name, $detached)) {
            header("location: " . $_POST['redirect']);
            exit();
        }
    }
    header("location: " . $_POST['redirect']);
} else if ($_GET) {
    $id = $_GET['id'];
    if (!empty($id)) {
        MarcasControl::Delete($id);
    }
    header("location: " . $_GET['redirect']);
}
?>
